/* eslint-disable no-param-reassign,no-undef */
import { routerRedux } from 'dva/router';

export default {
  namespace: 'login',
  state: {
    loginLoading: false,
  },
  reducers: {
    showLoginLoading(state) {
      return { ...state, loginLoading: true };
    },
    hideLoginLoading(state) {
      return { ...state, loginLoading: false };
    },
  },
  effects: {
    *login({ payload }, { put }) {
      const {
        token, userinfo, commonConst, systemData, decimals, dateFormat,
      } = payload;
      yield put({ type: 'app/saveToken', payload: token });
      yield put({ type: 'app/saveCommonConst', payload: commonConst });
      yield put({ type: 'app/saveUserinfo', payload: userinfo });
      yield put({ type: 'app/saveSystemData', payload: systemData });
      yield put({ type: 'app/saveDecimals', payload: decimals });
      yield put({ type: 'app/saveDateFormat', payload: dateFormat });

      yield put(routerRedux.push('indexPage'));
      // yield put({ type: 'showLoginLoading' });
      // const { value, url } = payload;
      // const { data } = yield call(loginService.postValueService, null, value, url);
      // if (data.code === 1) {
      //   const {
      //     token, gdslogininfo: userinfo, systemData, commonConst,
      //   } = data.dataset.rows[0];
      //   if (systemData !== undefined) {
      //     const dNetPrice = systemData.filter(item => (item.sName === 'NetPrice'))[0];
      //     const dNetMoney = systemData.filter(item => (item.sName === 'NetMoney'))[0];
      //     const decimals = {
      //       dNetPrice: dNetPrice.sValue !== undefined ? (dNetPrice.sValue) * 1 : 6,
      //       dNetMoney: dNetMoney.sValue !== undefined ? (dNetMoney.sValue) * 1 : 6,
      //     };
      //
      //   }
      //   // yield put({ type: 'app/savePanes', payload: { panes: [] } });
      //
      //   yield put({ type: 'app/saveDecimals', payload: decimals });
      //   yield put({ type: 'app/saveToken', payload: token });
      //   yield put({ type: 'app/saveUserinfo', payload: userinfo });
      //   yield put({ type: 'app/saveSystemData', payload: systemData });
      //   yield put({ type: 'app/saveCommonConst', payload: commonConst });
      //   yield put(routerRedux.push('indexPage'));
      // } else {
      //   yield put({ type: 'app/throwError', payload: data });
      // }
      // yield put({ type: 'hideLoginLoading' });
    },
  },
  subscriptions: {
  },
};
